<?php
namespace app\travel\controller;
use app\travel\model\TravelPlanModel;
use app\travel\model\TravelPlanItemModel;
use think\Controller;
use think\Request;
use think\Db;
class Travelplan extends Controller{
    private $TravelPlanModel;
    private $TravelPlanItemModel;
    public function __construct()
    {
        parent::__construct();
        $this->departmentid = $_SESSION['LOGIN_DEPT_ID'];
        $this->uid = $_SESSION['LOGIN_UID']; // 填报人id
        $this->uname = str_replace(',', '', iconv('gbk', 'UTF-8', GetUserNameByUid($this->uid))); // 填报人姓名
        $this->params = $this->request->param();
        $this->TravelPlanModel = new TravelPlanModel();
        $this->TravelPlanItemModel = new TravelPlanItemModel();
    }

    /*
     * 首页列表页
     */
    public function index()
    {
        return $this->fetch();
    }

    /*
     *  页面头部菜单添加
     */
    public function bar(){
        return $this->fetch();
    }

    /*
     * 月度出差单添加
     */
    public function add(){
        $user_name = $this->uname; // 申请人
        $user_department = $this->TravelPlanModel->GetDetpartmentNameByDtpId($this->departmentid);
        $this->assign('user_name', $user_name);
        $this->assign('user_department', $user_department);
        return $this->fetch();
    }

    /*
     *  添加出差明细
     */
    public function additem(){
        $data['creator'] = $this->uname; // 申请人
        $user_department = $this->TravelPlanModel->GetDetpartmentNameByDtpId($this->departmentid);
        $this->assign('user_department', $user_department);
        return $this->fetch();
    }

    /*
     * 月度出差单编辑
     */
    public function edit(){
        $id = $_REQUEST['id'];
        $result = $this->TravelPlanModel->GetPlanDetail($id);
        $this->assign('detail',$result);
        return $this->fetch();
    }

    /*
     * 出差明细编辑
     */
    public function edititem(){
        $id = $_REQUEST['id'];
        $result = $this->TravelPlanItemModel->GetPlanItemDetail($id);
        $user_department = $this->TravelPlanModel->GetDetpartmentNameByDtpId($this->departmentid);
        $this->assign('user_department', $user_department);
        $this->assign('detail',$result);
        return $this->fetch();
    }

    /*
     *  保存月度计划
     */
    public function save(){
        $parms = $this->params;
        $data['plan_num'] = $this->GetUniqueOrderId();
        $data['department_id'] = $this->departmentid;
        $data['user_id'] = $this->uid;
        $data['creator'] = $this->uname;
        $data['creator_id'] = $this->uid;
        $id = $this->TravelPlanModel->PlanSave($data,$parms);
        $plan_num = $this->TravelPlanModel->GetPlanNum($id['id']);
        $json = array("status" => 200, "msg" => "月度计划保存成功", 'data' => $id, 'plan_num' => $plan_num);
        return json_api($json);
    }

    /*
     *  更新月度计划
     */
    public function update(){
        $parms = $this->params;
        $this->TravelPlanModel->PlanUpdate($parms);
        $json = array("status" => 200, "msg" => "月度计划编辑成功");
        return json_api($json);
    }

    /*
     *  保存计划明细item
     */
    public function SaveItem(){
        $parms = $this->params;
        $parms['creator'] = $this->uname;
        $parms['creator_id'] = $this->uid;
        $parms['create_time'] = time();
        $parms['travel_status'] = "publish";
        $this->TravelPlanItemModel->PlanItemSave($parms);
        $json = array("status" => 200, "msg" => "月度计划明细新增成功");
        return json_api($json);
    }

    /*
     *  更新计划明细item
     */
    public function UpdateItem(){
        print_r($this->params);exit;
        $parms = $this->params;
        $parms['creator'] = $this->uname;
        $parms['creator_id'] = $this->uid;
        $parms['create_time'] = time();
        $parms['travel_status'] = "publish";
        $this->TravelPlanItemModel->PlanItemSave($parms);
        $json = array("status" => 200, "msg" => "月度计划明细新增成功");
        return json_api($json);
    }

    /*
     *  Ajax 月度计划表
     */
    public function GetListByAjax(){
        $uname = $this->uname;
        $page = $this->params['page'];
        $result = $this->TravelPlanModel->GetListByTraveler(25*($page-1),$uname);
        $count = $this->TravelPlanModel->GetPlanTotal($uname);
        $json = array("status" => 200, "msg" => "获取成功", "count" => $count, "data" => $result);
        return json_api($json);
    }

    /*
     *  明细表 保存
     */
    public function GetListByAjaxitem(){
        $result = $this->TravelPlanItemModel->GetItemList($this->params);
        $count = count($result);
        $json = array("status" => 200, "msg" => "获取成功", "count" => $count, "data" => $result);
        return json_api($json);
    }

    /*
     * 生成唯一月度单号
     */
    public function GetUniqueOrderId()
    {
        $prefix = "CC";
        $center = date('Ymd');
        $ymonth = date('Ym'); // 根据当前时间的年月 判定自增序列的条件 按年月度自增
        $lastid = $this->TravelPlanModel->GetUniquCode();
        $last_plan_num = $lastid;
        if (strpos($last_plan_num, $ymonth) !== false) { // 根据当前时间的年月与数据库中最新一条记录的出差编号对比 若包含 则表示同月份 自增加1
            $number = substr($last_plan_num, 10, 4);
            $pre = substr($last_plan_num, 0, 10);
            if(strlen($number+1) == 3){
                $fix = str_pad($pre,11, 0, STR_PAD_RIGHT).($number + 1);
            }elseif(strlen($number+1) == 4){
                $fix = str_pad($pre,10, 0, STR_PAD_RIGHT).($number + 1);
            }elseif(strlen($number+1) == 2){
                $fix = str_pad($pre,12, 0, STR_PAD_RIGHT).($number + 1);
            }else{
                $fix = str_pad($pre,13, 0, STR_PAD_RIGHT).($number + 1);
            }
        } else { // 若不包含 则从1开始自增
            $fix = $prefix.$center.str_pad('',3, 0, STR_PAD_RIGHT) . '1'; // 向右侧添加000
        }
        return $fix;
    }

    /*
     *  保存明细之后更新月度出差单总表
     */
    public function SaveToUpdate(){
        $department  = $this->TravelPlanModel->GetDetpartmentNameByDtpId($this->departmentid);
        $result = $this->TravelPlanModel->UpdateByItemTotal($department, $this->params);
        if($result){
            $json = array("status" => 200, "msg" => "更新月度总表成功", 'data' => $result);
            return json_api($json);
        }else{
            $json = array("status" => 500, "msg" => "更新月度总表失败", 'data' => $result);
            return json_api($json);
        }
    }
}